﻿using CefSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EDesk.cefwrapper
{
    internal class RenderProcessMessageHandler : IRenderProcessMessageHandler
    {
        /// <summary>
        /// 页面创建后，创建 EDeskReday 函数，封装JS对象绑定：CefSharp.BindObjectAsync('EDesk');
        /// 在Web页面中的使用方式：
        /// EDeskReady().then(() => {
        ///     EDesk.xxxx().then();
        /// });
        /// </summary>
        /// <param name="chromiumWebBrowser"></param>
        /// <param name="browser"></param>
        /// <param name="frame"></param>
        public void OnContextCreated(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
        {
            var initJs = @"
            var EDeskReady = function(){
	            return CefSharp.BindObjectAsync('EDesk');
            }
            ";
            frame.EvaluateScriptAsync(initJs);
            //frame.EvaluateScriptAsync(@"(async function() {await CefSharp.BindObjectAsync('EDesk');})();");
        }

        public void OnContextReleased(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
        {
            
        }

        public void OnFocusedNodeChanged(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IDomNode node)
        {
            
        }

        public void OnUncaughtException(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, JavascriptException exception)
        {
            
        }
    }
}
